/**
 * File Name	: DFNative.h
 * Location		: /Data/
 * Lib.Name		: Data.a
 * Designer		: OS (17-09-06)
 */

#include "stdafx.h"
#include "DFNative.h"
#include "../Core/System.h"

namespace Core
{

	DFNULL::DFNULL()
	{
		m_NumberOfItems = 0;
		Reset();
	}

	DFNULL::~DFNULL()
	{
	}

	void DFNULL::Reset()
	{
	}

	unsigned short DFNULL::GetLength() const
	{
		return 0;
	}

	void DFNULL::ReadRawData(Core::RawData &p_Data)
	{
	}

	void DFNULL::WriteRawData(Core::RawData &p_Data)
	{
	}

	DFUC::DFUC()
	{
		m_NumberOfItems = 1;
		Reset();
	}

	DFUC::DFUC(unsigned char p_UC)
	{
		m_NumberOfItems = 1;
		m_Value = p_UC;
	}

	DFUC::~DFUC()
	{
	}

	void DFUC::Reset()
	{
		m_Value = 0;
	}

	unsigned short DFUC::GetLength() const
	{
		return 1;
	}

	void DFUC::ReadRawData(Core::RawData &p_Data)
	{
		m_Value = p_Data.ReadChar();
	}

	void DFUC::WriteRawData(Core::RawData &p_Data)
	{
		p_Data.WriteChar(m_Value);
	}

	void DFUC::ReadJSON(Json::Value& pValue) {
		m_Value = (unsigned char)pValue.asUInt();
	}

	void DFUC::WriteJSON(Json::Value& pValue) {
		pValue = (Json::Value::UInt)m_Value;
	}

	DFUC::operator unsigned char()
	{
		return m_Value;
	}

	std::string DFUC::GetItemByIndex(unsigned char p_Index)
	{
		if(p_Index==0)
		{
			return System::ToString((int)m_Value);
		}
		return "";
	}

	void DFUC::SetItemByIndex(unsigned char p_Index, std::string p_String)
	{
		if(p_Index==0)
		{
			m_Value = atoi(p_String.c_str());
		}
	}

	DFSC::DFSC()
	{
		m_NumberOfItems = 1;
		Reset();
	}

	DFSC::DFSC(char t_SC)
	{
		m_NumberOfItems = 1;
		m_Value = t_SC;
	}

	DFSC::~DFSC()
	{
	}

	void DFSC::Reset()
	{
		m_Value = 0;
	}

	unsigned short DFSC::GetLength() const
	{
		return 1;
	}

	void DFSC::ReadRawData(Core::RawData &p_Data)
	{
		m_Value = (char)p_Data.ReadChar();
	}

	void DFSC::WriteRawData(Core::RawData &p_Data)
	{
		p_Data.WriteChar((unsigned char)m_Value);
	}

	void DFSC::ReadJSON(Json::Value& pValue) {
		m_Value = (char)pValue.asInt();
	}

	void DFSC::WriteJSON(Json::Value& pValue) {
		pValue = (Json::Value::Int)m_Value;
	}

	DFSC::operator char()
	{
		return m_Value;
	}

	std::string DFSC::GetItemByIndex(unsigned char p_Index)
	{
		if(p_Index==0)
		{
			return System::ToString((int)m_Value);
		}
		return "";
	}

	void DFSC::SetItemByIndex(unsigned char p_Index, std::string p_String)
	{
		if(p_Index==0)
		{
			m_Value = atoi(p_String.c_str());
		}
	}

	DFAUC::DFAUC(unsigned char p_Length)
	{
		m_NumberOfItems = 1;
		if(p_Length>32) p_Length=32;
		m_Length = p_Length;
		Reset();
	}

	DFAUC::~DFAUC()
	{
	}

	void DFAUC::Reset()
	{
		for(unsigned char i=0; i<m_Length; i++)
		{
			m_Value[i] = 0;
		}
	}

	void DFAUC::ReadRawData(RawData &p_Data)
	{
		for(unsigned char i=0; i<m_Length; i++)
		{
			m_Value[i] = p_Data.ReadChar();
		}
	}

	void DFAUC::WriteRawData(RawData &p_Data)
	{
		for(unsigned char i=0; i<m_Length; i++)
		{
			p_Data.WriteChar(m_Value[i]);
		}
	}

	void DFAUC::ReadJSON(Json::Value& pValue) {
        for(unsigned char i=0; i<m_Length; i++)
		{
			m_Value[i] = pValue.asString()[i];
		}
	}

	void DFAUC::WriteJSON(Json::Value& pValue) {
	    std::string temp = "";
		for(unsigned char i=0; i<m_Length; i++)
		{
			temp += m_Value[i];
		}
		pValue = temp;
	}

	unsigned char& DFAUC::operator[](unsigned char p_Index)
	{
		if(p_Index>m_Length) p_Index = m_Length;
		return m_Value[p_Index];
	}

	void DFAUC::operator=(std::string p_String)
	{
		Reset();
		unsigned char t_Length = m_Length;
		if(p_String.length()<t_Length) t_Length = (unsigned char) p_String.length();
		for(unsigned char i=0; i<t_Length; i++)
		{
			m_Value[i] = p_String[i];
		}
		if(t_Length<m_Length)
		{
			m_Value[t_Length] = 0;
		}
	}

	DFAUC::operator std::string()
	{
		std::string t_String;
		for(unsigned char i=0; i<m_Length; i++)
		{
			if(m_Value[i]==0)
			{
				break;
			}
			t_String += m_Value[i];
		}
		return t_String;
	}

	std::string DFAUC::GetItemByIndex(unsigned char p_Index)
	{
		if(p_Index==0)
		{
			return operator std::string();
		}
		return "";
	}

	void DFAUC::SetItemByIndex(unsigned char p_Index, std::string p_String)
	{
		if(p_Index==0)
		{
			operator=(p_String);
		}
	}

	DFUS::DFUS()
	{
		m_NumberOfItems = 1;
		Reset();
	}

	DFUS::DFUS(unsigned short p_US)
	{
		m_NumberOfItems = 1;
		m_Value = p_US;
	}

	DFUS::~DFUS()
	{
	}

	void DFUS::Reset()
	{
		m_Value = 0;
	}

	void DFUS::ReadRawData(Core::RawData &p_Data)
	{
		m_Value = p_Data.ReadShort();
	}

	void DFUS::WriteRawData(Core::RawData &p_Data)
	{
		p_Data.WriteShort(m_Value);
	}

	void DFUS::ReadJSON(Json::Value& pValue) {
		m_Value = (unsigned short)pValue.asUInt();
	}

	void DFUS::WriteJSON(Json::Value& pValue) {
		pValue = m_Value;
	}

	DFUS::operator unsigned short()
	{
		return m_Value;
	}

	std::string DFUS::GetItemByIndex(unsigned char p_Index)
	{
		if(p_Index==0)
		{
			return System::ToString((int)m_Value);
		}
		return "";
	}

	void DFUS::SetItemByIndex(unsigned char p_Index, std::string p_String)
	{
		if(p_Index==0)
		{
			m_Value = atoi(p_String.c_str());
		}
	}

	DFSS::DFSS()
	{
		m_NumberOfItems = 1;
		Reset();
	}

	DFSS::DFSS(short p_US)
	{
		m_NumberOfItems = 1;
		m_Value = p_US;
	}

	DFSS::~DFSS()
	{
	}

	void DFSS::Reset()
	{
		m_Value = 0;
	}

	void DFSS::ReadRawData(Core::RawData &p_Data)
	{
		m_Value = (short)p_Data.ReadShort();
	}

	void DFSS::WriteRawData(Core::RawData &p_Data)
	{
		p_Data.WriteShort((unsigned short)m_Value);
	}

	void DFSS::ReadJSON(Json::Value& pValue) {
		m_Value = (short)pValue.asInt();
	}

	void DFSS::WriteJSON(Json::Value& pValue) {
		pValue = m_Value;
	}

	DFSS::operator short()
	{
		return m_Value;
	}

	std::string DFSS::GetItemByIndex(unsigned char p_Index)
	{
		if(p_Index==0)
		{
			return System::ToString((int)m_Value);
		}
		return "";
	}

	void DFSS::SetItemByIndex(unsigned char p_Index, std::string p_String)
	{
		if(p_Index==0)
		{
			m_Value = atoi(p_String.c_str());
		}
	}

	DFU24::DFU24()
	{
		m_NumberOfItems = 1;
		Reset();
	}

	DFU24::DFU24(ubit p_UL)
	{
		m_NumberOfItems = 1;
		m_Value = p_UL;
	}

	DFU24::~DFU24()
	{
	}

	void DFU24::Reset()
	{
		m_Value = 0;
	}

	void DFU24::ReadRawData(Core::RawData &p_Data)
	{
		unsigned char t_Char;
		m_Value = 0;
		t_Char = p_Data.ReadChar();
		m_Value |= t_Char << 16;
		t_Char = p_Data.ReadChar();
		m_Value |= t_Char << 8;
		t_Char = p_Data.ReadChar();
		m_Value |= t_Char << 0;
	}

	void DFU24::WriteRawData(Core::RawData &p_Data)
	{
		unsigned char t_Char;
		t_Char = (m_Value >> 16) & 0xFF;
		p_Data.WriteChar(t_Char);
		t_Char = (m_Value >> 8) & 0xFF;
		p_Data.WriteChar(t_Char);
		t_Char = (m_Value >> 0) & 0xFF;
		p_Data.WriteChar(t_Char);
	}

	void DFU24::ReadJSON(Json::Value& pValue) {
		m_Value = (ubit)pValue.asUInt();
	}

	void DFU24::WriteJSON(Json::Value& pValue) {
		pValue = m_Value;
	}

	DFU24::operator ubit()
	{
		return m_Value;
	}

	std::string DFU24::GetItemByIndex(unsigned char p_Index)
	{
		if(p_Index==0)
		{
			return System::ToString((int)m_Value);
		}
		return "";
	}

	void DFU24::SetItemByIndex(unsigned char p_Index, std::string p_String)
	{
		if(p_Index==0)
		{
			m_Value = atoi(p_String.c_str());
		}
	}

	DFS24::DFS24()
	{
		m_NumberOfItems = 1;
		Reset();
	}

	DFS24::DFS24(sbit p_UL)
	{
		m_NumberOfItems = 1;
		m_Value = p_UL;
	}

	DFS24::~DFS24()
	{
	}

	void DFS24::Reset()
	{
		m_Value = 0;
	}

	void DFS24::ReadRawData(Core::RawData &p_Data)
	{
		unsigned char t_Char;
		m_Value = 0;
		t_Char = p_Data.ReadChar();
		m_Value |= t_Char << 16;
		t_Char = p_Data.ReadChar();
		m_Value |= t_Char << 8;
		t_Char = p_Data.ReadChar();
		m_Value |= t_Char << 0;
	}

	void DFS24::WriteRawData(Core::RawData &p_Data)
	{
		unsigned char t_Char;
		t_Char = (m_Value >> 16) & 0xFF;
		p_Data.WriteChar(t_Char);
		t_Char = (m_Value >> 8) & 0xFF;
		p_Data.WriteChar(t_Char);
		t_Char = (m_Value >> 0) & 0xFF;
		p_Data.WriteChar(t_Char);
	}

	void DFS24::ReadJSON(Json::Value& pValue) {
		m_Value = (sbit)pValue.asInt();
	}

	void DFS24::WriteJSON(Json::Value& pValue) {
		pValue = m_Value;
	}

	DFS24::operator sbit()
	{
		return m_Value;
	}

	std::string DFS24::GetItemByIndex(unsigned char p_Index)
	{
		if(p_Index==0)
		{
			return System::ToString((int)m_Value);
		}
		return "";
	}

	void DFS24::SetItemByIndex(unsigned char p_Index, std::string p_String)
	{
		if(p_Index==0)
		{
			m_Value = atoi(p_String.c_str());
		}
	}

	DFUL::DFUL()
	{
		m_NumberOfItems = 1;
		Reset();
	}

	DFUL::DFUL(unsigned long p_UL)
	{
		m_NumberOfItems = 1;
		m_Value = p_UL;
	}

	DFUL::~DFUL()
	{
	}

	void DFUL::Reset()
	{
		m_Value = 0;
	}

	void DFUL::ReadRawData(Core::RawData &p_Data)
	{
		m_Value = p_Data.ReadLong();
	}

	void DFUL::WriteRawData(Core::RawData &p_Data)
	{
		p_Data.WriteLong(m_Value);
	}

	void DFUL::ReadJSON(Json::Value& pValue) {
	    m_Value = pValue.asUInt();
	}

    void DFUL::WriteJSON(Json::Value& pValue) {
        pValue = (Json::Value::UInt) m_Value;
    }

	DFUL::operator unsigned long()
	{
		return m_Value;
	}

	std::string DFUL::GetItemByIndex(unsigned char p_Index)
	{
		if(p_Index==0)
		{
			return System::ToString((int)m_Value);
		}
		return "";
	}

	void DFUL::SetItemByIndex(unsigned char p_Index, std::string p_String)
	{
		if(p_Index==0)
		{
			m_Value = atoi(p_String.c_str());
		}
	}

	DFSL::DFSL()
	{
		m_NumberOfItems = 1;
		Reset();
	}

	DFSL::DFSL(long p_SL)
	{
		m_NumberOfItems = 1;
		m_Value = p_SL;
	}

	DFSL::~DFSL()
	{
	}

	void DFSL::Reset()
	{
		m_Value = 0;
	}

	void DFSL::ReadRawData(Core::RawData &p_Data)
	{
		m_Value = (long)p_Data.ReadLong();
	}

	void DFSL::WriteRawData(Core::RawData &p_Data)
	{
		p_Data.WriteLong((unsigned long)m_Value);
	}

	void DFSL::ReadJSON(Json::Value& pValue) {
		m_Value = (long)pValue.asInt();
	}

	void DFSL::WriteJSON(Json::Value& pValue) {
		pValue = (Json::Value::Int)m_Value;
	}

	DFSL::operator long()
	{
		return m_Value;
	}

	std::string DFSL::GetItemByIndex(unsigned char p_Index)
	{
		if(p_Index==0)
		{
			return System::ToString((int)m_Value);
		}
		return "";
	}

	void DFSL::SetItemByIndex(unsigned char p_Index, std::string p_String)
	{
		if(p_Index==0)
		{
			m_Value = atoi(p_String.c_str());
		}
	}

	DFString::DFString()
	{
		m_NumberOfItems = 1;
		Reset();
	}

	DFString::DFString(std::string p_String)
	{
		m_NumberOfItems = 1;
		m_Value = p_String;
	}

	DFString::~DFString()
	{
	}

	void DFString::Reset()
	{
		m_Value = "";
	}

	unsigned short DFString::GetLength() const
	{
		return 1;
	}

	void DFString::ReadRawData(Core::RawData &p_Data)
	{
		std::ostringstream t_String;
		unsigned char t_Length = p_Data.ReadChar();
		for(unsigned char i=0; i<t_Length; i++)
			t_String << p_Data.ReadChar();
		m_Value = t_String.str();
	}

	void DFString::WriteRawData(Core::RawData &p_Data)
	{
		unsigned char t_Length;
		if(m_Value.length()>0xFF)
			t_Length = 0xFF;
		else
			t_Length = (unsigned char) m_Value.length();
		p_Data.WriteChar(t_Length);
		for(unsigned char i=0; i<t_Length; i++)
			p_Data.WriteChar(m_Value[i]);
	}

	void DFString::ReadJSON(Json::Value& pValue) {
        m_Value = pValue.asString();
	}

	void DFString::WriteJSON(Json::Value& pValue) {
        pValue = m_Value;
	}

	DFString::operator std::string()
	{
		return m_Value;
	}

	std::string DFString::GetItemByIndex(unsigned char p_Index)
	{
		if(p_Index==0)
		{
			return m_Value;
		}
		return "";
	}

	void DFString::SetItemByIndex(unsigned char p_Index, std::string p_String)
	{
		if(p_Index==0)
		{
			m_Value = p_String;
		}
	}

}
